<?php $header=$this->action('Application\Controller\IndexController', 'headerAction',array('20','10'));
$baseUrl = $header->headerarray['baseUrl'];
$basePath = $header->headerarray['basePath'];
?>
<script> 
var BASE_PATH = '<?php echo $basePath;?>';
var BASE_URL = '<?php echo $baseUrl;?>';
</script>	
<? if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="") {?>
<script>
	<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="user") {?>
		window.location='/users/user-previous?user_id=<? echo $_SESSION['user']['user_id'];?>';
	<? } else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin") {?>
		window.location='/users/user-previous?user_id=<? echo $_SESSION['user']['user_id'];?>';
	<? } else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin") {?>
		window.location='/users/user-previous?user_id=<? echo $_SESSION['user']['user_id'];?>';
	<?}?>	
</script>
<?} else {?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	</head>
	<body>
		<div class="bg-cover"></div>
		<div id="wrapper" class="container">
			<section class="login">
				<h1>Forgetpassword</h1>
					<div class="login_sub">
						<div class="login_input">
							<input type="text" id="forgetmail" name="forgetmail" placeholder="User email"/>
						</div>
						<div id="erroremail"  style="color:red;width:266px;margin:0 auto;" ></div>							
						<div id="erroremail1"  style="color:red;width:266px;margin:0 auto;" ></div>

						<div class="sign_in">
							<a href="#" onclick="return forgetPassword();" class="btn btn-danger" role="button">Submit</a>
							<a href="<? echo $this->Url('home');?>" class="btn btn-danger" role="button">Back</a>
							<div id="loading" style="display:none;float:right;margin-right:30px;"><img src="<?php echo $this->basePath() ?>/images/loading_image.gif" /></div>
						</div>					
					</div>
			</section>			
		</div>
		
	</body>
</html>
<div class="clear"></div>  
<script type="text/javascript" charset="utf-8">	
$('#forgetmail').keypress(function(e){
	if( e.keyCode == 13 ){
		forgetPassword();	
	}
});
function forgetPassword(){
	var flag=true;	
	var flag=true;	
	var emailcheck=$('#forgetmail').val();	
	if($('#forgetmail').val()==""){		
		$('#erroremail').html('*Email required');
		flag=false;
	}
	if(checkEmail(emailcheck)==false)
	{		
		$('#erroremail').html('*Email format is not correct');
		flag=false;
	}
	if(flag==false){ 
		return false;
	}else{	
		$.ajax({
		type:'POST',
		url:  BASE_PATH+'/users/checkregemail',
		data:{email:emailcheck},
			success: function(data){
				if(data.output=='success'){
					$('#erroremail1').html('');
					$('#loading').show();
					$.ajax({
						type:'POST',
						url: BASE_PATH+'/users/forgetpassword',
						data:{email:emailcheck},
						success: function(data){
							if(data.output=='success'){	
								$('#loading').hide();	
								$('#erroremail').html('You will receive a mail for a new Password.');								
							}else{
								$('#erroremail').html('Not sucessfully');
							}
						}
					});
				}else{
					$('#erroremail1').html('Entered email is not valid email');
				}					
			}			
		});				
	}		
}
function checkEmail(emailStr) {
	if (emailStr.length == 0) {
		return true;
	}
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
	var atom=validChars + "+";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat) == null) {
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
			}
		}
		return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray == null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if ((domArr[domArr.length-1].length < 2) ||(domArr[domArr.length-1].length > 3)) {
		return false;
	}
	if (len < 2) {
	   return false;
	}
	return true;
}
</script>
<?}?>